<?php
/**
 * Created by PhpStorm.
 * User: winleung
 * Date: 2019-10-22
 * Time: 23:11
 */

namespace app\api\model;

use think\Model;
use think\Db;


class PharmacyModel extends Model
{

    protected $table = 'pharmacy_account';

    /**
     * 根据药店的手机号码  密码 做登录
     * @param $mobilePhone
     * @param $password
     * @return array|\think\db\concern\Model
     */
    public function fetchPharmacy($mobilePhone, $password)
    {
        $pharmacy = Db::name($this->table)->where('pharmacy_mobile_phone', $mobilePhone)
            ->where('password', $password)->find();

        return $pharmacy;
    }

    /**
     * 更新登录的时候的openid
     * @param $userId
     * @param $openId
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updatePharmacyAccountOpneid($userId, $openId){
        $resultCount = Db::name($this->table)
            ->where('id', $userId)

            ->update(['login_openid' => $openId]);

        if ($resultCount > 0) {
            //更新失败
            return true;
        } else {

            return false;
        }

    }

    /**
     *
     * @param $userId
     * @param $longitude
     * @param $latitude
     * @param $address
     * @param $businessLicenseImg
     * @param $licensedPharmacistLicenseImg
     * @param $drugTradingLicenseImg
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function addCheckPharmacyInfo($userId, $longitude, $latitude, $address, $businessLicenseImg, $licensedPharmacistLicenseImg, $drugTradingLicenseImg)
    {

        $resultCount = Db::name($this->table)
            ->where('id', $userId)
            ->where('check_status', 'in', [3, 4])
            ->update(['longitude' => $longitude, 'latitude' => $latitude, 'address' => $address, 'business_license_img' => $businessLicenseImg, 'licensed_pharmacist_license_img' => $licensedPharmacistLicenseImg, 'drug_trading_license_img' => $drugTradingLicenseImg]);

        if ($resultCount > 0) {
            //更新失败
            return true;
        } else {

            return false;
        }

    }

    /**
     * 更新审核状态
     * @param $userId
     * @param $checkStatus
     * @return bool
     * @throws \think\db\exception\DbException
     */
    public function updateCheckStatus($userId, $checkStatus)
    {
        if (!in_array($checkStatus, [2, 3])) {
            //状态变更不正常
            return false;
        }

        $resultCount = Db::name($this->table)
            ->where('id', $userId)
            ->where('check_status', 1)
            ->update(['check_status' => $checkStatus]);

        return 0 > $resultCount;
    }

    /**
     * 获取药店派单数量
     */
    public function fetchPatchOrderCount($pharmacyId)
    {

        $count = Db::name($this->table)->where('id', $pharmacyId)->count('id');

        return $count;

    }

    public function getPharmacyInfoById($pharmacyId)
    {
        $result = null;
        try {
            $result = Db::name($this->table)
                ->where('id', $pharmacyId['pharmacy_id'])
                ->where('check_status', '2')
                ->field("id, pharmacy_name, pharmacy_mobile_phone, pharmacy_tel_phone, contact_name, city_name, area_name, address, longitude, latitude, avatar")
                ->find();
        } catch (Exception $e) {
            throwBusinessException("订单分配不存在");
        }

        return $result;


    }



    /**
     * 获取已审核通过的药店
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPharmacyInfoList(){

        $data = Db::name($this->table)
            ->where('check_status', 2)
            ->select();

        return $data;


    }

    /**
     * 通过openid 获取药店的登录信息
     * @param $openId
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getPharmacyInfoByOpenId($openId){

        $data = Db::name($this->table)
            ->where('check_status', 2)
            ->where('login_openid', $openId)
            ->find();

        if(empty($data)){
            return false;
        }

        $data = $data->toArray();

        if(empty($data)){
            return false;
        }

        return $data;


    }
}